// hungvjnh
bstoMenuSelect = function(element){
	var selected = $("#table_select"+element+" option:selected").val();
	if(selected == 'user'){// user -> username | email | phone | company | address | type
		newOptions = {
			    'username' 	: 'Username',
			    'email' 	: 'Email',
			    'phone' 	: 'Phone',
			    'company' 	: 'Company',
			    'address'	: 'Address',
			    'type'		: 'Type'
			};
		
		selectedOption = 'username';
	}
	else if(selected == 'book'){// book -> summary | detail | title
		newOptions = {
				'summary'	: 'Summary',
				'detail'	: 'Detail',
				'title'		: 'Title'
		};
		
		selectedOption = 'summary';
	}
	else if(selected == 'static'){// static -> detail | title
		newOptions = {
				'detail'	: 'Detail',
				'title'		: 'Title'
		};
		selectedOption = 'detail';
	}
	else if(selected == 'category'){// category -> name
		newOptions = {
				'name'	: 'Name'
		};
		selectedOption = 'name';
	}
	else if(selected == 'config'){// config -> key
		newOptions = {
				'key'	: 'Key'
		};
		selectedOption = 'key';
	}
	else {
		newOptions = {};
		selectedOption = '';
	}

	var select = $('#attr_select'+element);
	if(select.prop) {
	  var options = select.prop('options');
	}
	else {
	  var options = select.attr('options');
	}
	$('option', select).remove();

	$.each(newOptions, function(val, text) {
	    options[options.length] = new Option(text, val);
	});
	select.val(selectedOption);
	$('#table_select'+element).val(selected);

}

//hungvjnh
bstoMenuFunc = function(){
	$("#table_select").change(function(){
    	bstoMenuSelect('');
    });
	$("#table_select2").change(function(){
    	bstoMenuSelect('2');
    });
	/*$("#attr_select").change(function(){
		var selected = $("#attr_select option:selected").val(); 
		 $("#attr_select2").val(selected);
	});
	$("#attr_select2").change(function(){
		var selected = $("#attr_select2 option:selected").val(); 
		 $("#attr_select").val(selected);
	});*/
}

$(function(){

	$('input').checkBox();
	$('#toggle-all').click(function(){
     	$('#toggle-all').toggleClass('toggle-checked');
    	$('#mainform input[type=checkbox]').checkBox('toggle');
   	    return false;
	});



	var777 = $('.styledselect_form_1').selectbox({ inputClass: "styledselect_form_1" });
	$('.styledselect_form_2').selectbox({ inputClass: "styledselect_form_2" });

    
	$('.styledselect_pages').selectbox({ inputClass: "styledselect_pages" });

	$("input.file_1").filestyle({ 
    	//image: "views/images/forms/upload_file.gif",
        image: varUploadBtn,
    	imageheight : 29,
    	imagewidth : 78,
    	width : 300
	});


	$('a.info-tooltip ').tooltip({
		track: true,
		delay: 0,
		fixPNG: true, 
		showURL: false,
		showBody: " - ",
		top: -35,
		left: 5
	});


// initialise the "Select date" link

    $('#date-pick')
    .datePicker(
		// associate the link with a date picker
		{
			createButton:false,
			startDate:'01/01/2005',
			endDate:'31/12/2020'
		}
	).bind(
		// when the link is clicked display the date picker
		'click',
		function()
		{
			updateSelects($(this).dpGetSelected()[0]);
			$(this).dpDisplay();
			return false;
		}
	).bind(
		// when a date is selected update the SELECTs
		'dateSelected',
		function(e, selectedDate, $td, state)
		{
			updateSelects(selectedDate);
		}
	).bind(
		'dpClosed',
		function(e, selected)
		{
			updateSelects(selected[0]);

		}
	);

	

    var updateSelects = function (selectedDate)
    {
    	var selectedDate = new Date(selectedDate);
    	$('#d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
    	$('#m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
    	$('#y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
    
    }

// listen for when the selects are changed and update the picker

    $('#d, #m, #y')
	.bind(
		'change',
		function()
		{
			var d = new Date(
						$('#y').val(),
						$('#m').val()-1,
						$('#d').val()
					);
			$('#date-pick').dpSetSelected(d.asString());
		}
	);

// default the position of the selects to today
    var today = new Date();
    updateSelects(today.getTime());



// and update the datePicker to reflect it...

    $('#d').trigger('change');
    
    $(document).pngFix( );



});

$(document).ready(function () {
	bstoMenuFunc();
	//bstoMenuSelect('');
});


